package com.ecloud.arithmetic.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ecloud.arithmetic.dao.EnergyIndicatorsMonthMapper;
import com.ecloud.arithmetic.dto.EnergyIndicatorsMonthDto;
import com.ecloud.arithmetic.dto.EnergyIndicatorsMonthSelectDto;
import com.ecloud.arithmetic.entities.EnergyIndicatorsMonth;
import com.ecloud.arithmetic.service.EnergyIndicatorsMonthService;
import com.ecloud.common.ResultEnum;
import com.ecloud.common.exception.ServerException;
import com.ecloud.common.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Squbi
 */
@Service
public class EnergyIndicatorsMonthServiceImpl extends ServiceImpl<EnergyIndicatorsMonthMapper, EnergyIndicatorsMonth> implements EnergyIndicatorsMonthService {
    @Autowired
    EnergyIndicatorsMonthMapper monthMapper;

    @Override
    public void insert(EnergyIndicatorsMonthDto dto) {
        EnergyIndicatorsMonth em;
        try {
            em = dto.toEntities(new EnergyIndicatorsMonth());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            throw new ServerException(ResultEnum.ARGS_ERR, e.getMessage());
        }
        em.setDataTime(TimeUtil.fromMonthStr(dto.getDataMonthTime()));
        em.setCreateTime(LocalDateTime.now());
        monthMapper.insert(em);
    }

    @Override
    public List<EnergyIndicatorsMonth> select(EnergyIndicatorsMonthSelectDto dto) {
        dto.toLimit();
        return monthMapper.selectPage(dto);
    }
}
